package com.example.appkcsj.slice;

import com.example.appkcsj.ResourceTable;
import com.example.appkcsj.dbutils.DBUtils;
import com.example.appkcsj.entity.Student;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class maddAbilitySlice extends AbilitySlice implements Component.ClickedListener{
        private final static HiLogLabel HI_LOG_LABEL=
        new HiLogLabel(HiLog.LOG_APP,0x00201,"maddAbilitySlice");
        private TextField musername1;
        private TextField mpassword1;
        private TextField mpassword2;
        private Button mreg1;
        private DBUtils dbUtils;
        @Override
        public void onStart(Intent intent) {
            super.onStart(intent);
            super.setUIContent(ResourceTable.Layout_ability_madd);

            dbUtils=new DBUtils(com.example.appkcsj.slice.maddAbilitySlice.this);
            musername1= (TextField) findComponentById(ResourceTable.Id_fieldm1);
            mpassword1= (TextField) findComponentById(ResourceTable.Id_fieldm2);
            mpassword2= (TextField) findComponentById(ResourceTable.Id_fieldm3);
            mreg1 =(Button)findComponentById(ResourceTable.Id_butm1);
            mreg1.setClickedListener(this);
            mreg1.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    if(component == mreg1){
                        if("".equals(musername1.getText())||"".equals(mpassword1.getText()))
                        {
                            ToastDialog td = new ToastDialog(com.example.appkcsj.slice.maddAbilitySlice.this);
                            td.setText("请输入用户名及密码！");
                            td.show();
                        }
                        else {
                            if (mpassword2.getText().equals(mpassword1.getText())) {
                                Student student = new Student(musername1.getText(), mpassword1.getText(), mpassword2.getText());
                                boolean flag = dbUtils.insertStudent(student);
                                if (flag) {
                                    HiLog.info(HI_LOG_LABEL, "--插入数据->>" + flag);
                                    ToastDialog td = new ToastDialog(com.example.appkcsj.slice.maddAbilitySlice.this);
                                    td.setText("增加成功！");
                                    td.show();
                                    Intent m = new Intent();
                                    m.setParam("musername1",musername1.getText());
                                    m.setParam("mpassword1",mpassword1.getText());
                                    m.setParam("mpassword2",mpassword2.getText());
                                    present(new MainAbilitySlice(),m);
                                    Operation operation = new Intent.OperationBuilder()
                                            .withDeviceId("")
                                            .withBundleName("com.example.appkcsj")//跳转到哪个应用
                                            .withAbilityName("com.example.appkcsj.maddAbility")//要跳转的包名
                                            .build();//将上面的信息打包
                                    m.setOperation(operation);
                                    startAbility(m);
                                } else {
                                    HiLog.info(HI_LOG_LABEL, "--插入数据->>" + flag);
                                    ToastDialog td = new ToastDialog(com.example.appkcsj.slice.maddAbilitySlice.this);
                                    td.setText("增加失败！");
                                    td.show();
                                }
                            }
                            else{
                                if("".equals(mpassword2.getText())){
                                    ToastDialog td = new ToastDialog(com.example.appkcsj.slice.maddAbilitySlice.this);
                                    td.setText("请确认密码！");
                                    td.show();
                                }
                                else{
                                    ToastDialog td = new ToastDialog(com.example.appkcsj.slice.maddAbilitySlice.this);
                                    td.setText("两次密码不一致！");
                                    td.show();}
                            }
                        }
                    } }
            });

        }
        @Override
        public void onActive() {
            super.onActive();
        }
        @Override
        public void onForeground(Intent intent) {
            super.onForeground(intent);
        }
        @Override
        public void onClick(Component component) {}
    }
